package com.atsansan.mywork.login.pachong;


import com.atsansan.mywork.login.entity.CourseraEntity;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author sansan
 * @create 2021-04-08 21:45
 */
@Component
public class CourseraInfo {

//    private static String s="Game Programming/Linear Algebra/Manufacturing Process Management/Long Short-Term Memory/" +
//        "Denial-Of-Service Attacks/Electronics/Reinforcement Learning/Advertising/Combine/Hardware Design/Jpeg/" +
//        "Facial/Primitive Data Type/Python Syntax And Semantics/Algorithmics/Architecture/Cloud Platforms/" +
//        "Interactivity/Systems Design/Surround Sound/Clinical Study Design/Porting/Regression/Head-Up Display/" +
//        "Array Data Structure/Computer Networking/Information Technology Security Audit/Telecommunications Network/" +
//        "Inequality (Mathematics)/Training/Version Control/Cisco/Variable (Computer Science)/Global/Adult Learner/" +
//        "Relational Database/" + "Jenkins (Software)/Software Versioning/Personalization/Bigquery/Project Mine/" +
//        "Open Source/File Shortcut/3d Modeling/Creativity/Hypertext Transfer Protocol/Semantics/For Loop/Test Set/" +
//        "C Sharp (Programming Language)/Dynamic Programming/Databases/C Dynamic Memory Allocation/Deep Learning/" +
//        "Linearity/Convolution/Javascript Syntax/React (Web Framework)/Blockchain/Stack (Abstract Data Type)/Blockchain/" +
//        "Cryptography/.Properties/User Research/Autoencoder/Smart Contract/Video Game Development/Sorting/" +
//        "Object-Oriented Programming/Microsoft Excel/Recurrent Neural Network/Computer Vision/Game Design/" +
//        "Image Processing/Internet Protocol/Mapping/Command-Line Interface/Unix Shells/Reinforcement/" +
//        "Digital Signal Processing/Abstraction/Approximation Error/Artificial Neural Networks/Null Coalescing Operator/Resource/Text Box/E-Nable/Map/Cipher/Hashing/Proof-Of-Work System/Objective-C/I-Deas/Strategy/Web Development/" +
//        "User Experience Design/Computer Program/Software Security/Android Development/Entry Point/Machine Learning" +
//        "/Test Automation/Access Control/Test Case/Provisioning/Product Management/Upload/Interview/Principle/" +
//        "Logistic Regression/Experience/User Interface/Editing/Art/Python Programming/Rendering (Computer Graphics)/" +
//        "Interaction Design/Javascript/Google App Engine/Computer Programming/Network Security/Accuracy And Precision/" +
//        "Display Devices/Source Code/Information Security/Agile Management/Crt0/Extract!, Transform!, Load/" +
//        "While Loop/Project/Deployment Environment/Internet/Mathematical Optimization/Iteration/Sources/" +
//        "File System/Usability/C Programming/Customer Experience/Software Testing/Flow Network";
    private static String s="Switches/Internality/Ibm Cloud/Bitcoin/Scrum (Software Development)/Framing/Leadership And Management/" +
        "Breach (Security Exploit)/Logic/Github/Exception Handling/Internet Of Things";
//    public static void main(String[] args) throws IOException{
//        getCourseras();
//
//
//    }
//    public static void main(String[] args) throws IOException{
//        List<String> cImgs = getImg(url);
//        List<String> listName = getName(url);
//        List<String> listUniversity = getUniversity(url);
//        List<String> listLink = getLink(url);
//        String[] levles = s.split("/");
//
//        String urls = s.replace(" ", "+");
//        List<String> listLevel=Arrays.asList(levles);
//        List<String> listUrl=new ArrayList<>();
//        for (String slevle:listLevel){
//            String url = slevle.replace(" ", "+");
//            listUrl.add(url);
//        }
//
//    }
    private static String urlStart="https://www.coursera.org/browse/computer-science?facets=skillNameMultiTag%3A";
    private static String urlEnd="%2Clanguages%3AEnglish%2CcategoryMultiTag%3Acomputer-science";
    private static String url1=null;

    public static List<CourseraEntity> getCourseras() throws IOException {
//        levles是空格的
        String[] levles = s.split("/");
//        urls是+的
        String urls = s.replace(" ", "+");
        List<String> listLevel=Arrays.asList(levles);
        List<String> listUrl=new ArrayList<>();
//        listUrls是完整的url
        List<String> listUrls=new ArrayList<>();
        for (String slevle:listLevel){
            String url = slevle.replace(" ", "+");
            listUrl.add(url);
        }
        for (String surl:listUrl){
            url1=urlStart+surl+urlEnd;
            listUrls.add(url1);
        }

        for (String urlss:listUrls){
            System.out.println(urlss);
        }
        List<CourseraEntity> courseraEntities=new ArrayList<>();

//        for (int i=0;i<listUrls.size();i++){
//            List<String> cImgs = getImg(listUrls.get(i));
//            List<String> listName = getName(listUrls.get(i));
//            List<String> listUniversity = getUniversity(listUrls.get(i));
//            List<String> listLink = getLink(listUrls.get(i));
//            String level=listLevel.get(i);
//            for (int j = 0; j <listName.size() ; j++) {
////            需要创建listAuthor.size()个对象,对每个对象进行赋值
//                CourseraEntity courseraEntity= new CourseraEntity();
//                courseraEntity.setCImg(cImgs.get(j));
//                courseraEntity.setCName(listName.get(j));
//                courseraEntity.setCUniversity(listUniversity.get(j));
//                courseraEntity.setCLink(listLink.get(j));
//                courseraEntity.setCLevel(level);
//                courseraEntities.add(courseraEntity);
//
//            }
//        }



        return courseraEntities;
    }
    public static List<CourseraEntity> getCoursera(String url) throws IOException {


        List<CourseraEntity> courseraEntities=new ArrayList<>();


        List<String> cImgs = getImg(url);
        List<String> listName = getName(url);
        List<String> listUniversity = getUniversity(url);
        List<String> listLink = getLink(url);
        String replace1=url.replace("https://www.coursera.org/browse/computer-science?facets=skillNameMultiTag%3A","");
        String replace2 = replace1.replace("%2Clanguages%3AEnglish%2CcategoryMultiTag%3Acomputer-science", "");
        String level=replace2.replace("+"," ");
        for (int j = 0; j <cImgs.size() ; j++) {
//            需要创建listAuthor.size()个对象,对每个对象进行赋值
            CourseraEntity courseraEntity= new CourseraEntity();
            courseraEntity.setCImg(cImgs.get(j));
            courseraEntity.setCName(listName.get(j));
            courseraEntity.setCUniversity(listUniversity.get(j));
            courseraEntity.setCLink(listLink.get(j));
            courseraEntity.setCLevel(level);
            courseraEntities.add(courseraEntity);

        }
        return courseraEntities;
    }
    public static List<String> getLink(String s) throws IOException {
        Document documentLink = null;
        Elements elementsLink=null;
        List<String> listLink =new ArrayList<>();
        String clink=null;
        Document document = Jsoup.connect(s).get();

        Elements elements = document.select("a._dtnmh78");
        documentLink=Jsoup.parse(String.valueOf(elements));
        elementsLink=documentLink.getElementsByClass("_dtnmh78");

//        System.out.println(documentLink);
//        System.out.println(elementsLink);
        for (Element elements1:elementsLink){
            clink=elements1.attr("href");
            listLink.add(clink);
        }
//        System.out.println(listLink.size());
//        System.out.println(listLink.toString());
        return listLink;
    }

    public static List<String> getName(String s) throws IOException {
        Document documentName = null;
        Elements elementsName=null;
        List<String> listName =new ArrayList<>();
        String cName=null;
        Document document = Jsoup.connect(s).get();

        Elements elements = document.select("h4.product-name");
        documentName=Jsoup.parse(String.valueOf(elements));
        elementsName=documentName.select("h4.product-name");

//        System.out.println(documentName);
//        System.out.println(elementsName);
        for (Element elements1:elementsName){
            cName=elements1.text();
            listName.add(cName);

        }
//        System.out.println(listName.size());
//        System.out.println(listName.toString());
        return listName;
    }
    public static List<String> getImg(String s) throws IOException {
        Document documentImg = null;
        Elements elementsImg = null;
        List<String> cImgs =new ArrayList<>();
        String cImg=null;
        Document document = Jsoup.connect(s).get();

        Elements elements = document.select("img.product-photo");
        documentImg = Jsoup.parse(String.valueOf(elements));
        elementsImg = documentImg.getElementsByClass("product-photo");

//        System.out.println(documentImg);
//        System.out.println(elementsImg);
        for (Element elements1 : elementsImg) {
            cImg = elements1.attr("src");
            cImgs.add(cImg);
//            System.out.println(cImg);


        }
//        System.out.println(cImgs.size());
//        System.out.println(bImgs.toString());
        return cImgs;


    }
    public static List<String> getUniversity(String s) throws IOException {
        Document documentUniversity = null;
        Elements elementsUniversity= null;

        String university = null;
        List<String> listUniversity =new ArrayList<>();
        Document document = Jsoup.connect(s).get();

//        div class="a-row a-size-base a-color-secondary"
//        span class="a-size-base"
        Elements elements = document.select("h5.partner-name");
        documentUniversity = Jsoup.parse(String.valueOf(elements));

        elementsUniversity=documentUniversity.select("h5.partner-name");
        for (Element elements1:elementsUniversity){
            university=elements1.text();
            listUniversity.add(university);

        }
//        System.out.println(listUniversity.size());
//        System.out.println(listUniversity.toString());


        return listUniversity;


    }




    public static List<String> getLevel(String s) throws IOException {
        Document documentLevel = null;
        Elements elementsLevel=null;
        List<String> listLevel =new ArrayList<>();
        String cLevel=null;
        Document document = Jsoup.connect(s).get();
        Elements elements = document.select("input._htmk7zm");
        documentLevel=Jsoup.parse(String.valueOf(elements));
        elementsLevel=documentLevel.select("input._htmk7zm");

//        System.out.println(documentName);
//        System.out.println(elementsName);
        for (Element elements1:elementsLevel){
            cLevel=elements1.text();
            listLevel.add(cLevel);

        }
        System.out.println(listLevel.size());
        System.out.println(listLevel.toString());
        return listLevel;
    }
}
